package com.lsh.oms.provider.service.order;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.alibaba.fastjson.JSON;
import com.lsh.base.common.json.JsonUtils;
import com.lsh.base.common.model.CommonResult;
import com.lsh.oms.api.model.order.receipt.BatchReceiptHeadDto;
import com.lsh.oms.api.model.order.receipt.ReceiptHeadDto;
import com.lsh.oms.api.model.order.sign.SignHeadDto;
import com.lsh.oms.api.service.order.IOrderReceiptRestService;
import com.lsh.oms.core.constant.ReturnCode;
import com.lsh.oms.core.exception.BusinessException;
import com.lsh.oms.core.exception.ExceptionStatus;
import com.lsh.oms.core.model.order.OrderShippingHead;
import com.lsh.oms.core.service.order.OrderReceiptService;
import com.lsh.oms.core.service.other.ShippingOrderService;
import com.lsh.oms.core.service.tool.ToolService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

/**
 * Project Name: lsh-oms
 *
 * @author peter
 * @date 18/6/24
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Service(protocol = "rest", timeout = 30000)
@Path("saas/order/receipt")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML, MediaType.APPLICATION_FORM_URLENCODED, MediaType.MULTIPART_FORM_DATA})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
@Slf4j
public class OrderReceiptRestServiceImpl implements IOrderReceiptRestService {

    @Autowired
    private OrderReceiptService orderReceiptService;

    @Autowired
    private ShippingOrderService shippingService;

    @GET
    @Path("full/create")
    @Override
    public CommonResult<Object> createFull(@QueryParam("orderCode") Long orderCode) {

        OrderShippingHead shippingHead = shippingService.selectOneByOrderCode(orderCode, false);
        if (null == shippingHead) {
            return CommonResult.error("发货单信息不存在", false);
        }

        ReceiptHeadDto receiptHeadDto = new ReceiptHeadDto();
        receiptHeadDto.setShippingOrderId(shippingHead.getShippingOrderId());

        return create(receiptHeadDto);
    }

    @POST
    @Path("create")
    @Override
    public CommonResult<Object> create(ReceiptHeadDto receiptHeadDto) {
        String logSign = "logSign : " + ToolService.getLogSign();

        log.info("saas order receipt create " + JSON.toJSONString(receiptHeadDto));
        CommonResult<Object> commonResult = new CommonResult<>();
        try {
            this.checkPara(receiptHeadDto, logSign);

            commonResult = this.orderReceiptService.createReceiptOrder(receiptHeadDto, logSign);

            log.info(logSign + "commonResult-receipt- shippingOrderId {}, 结果 {}" + receiptHeadDto.getShippingOrderId(), JSON.toJSONString(commonResult));
        } catch (BusinessException e) {
            commonResult.setCode(e.getCode());
            commonResult.setMessage(e.getMessage());
            log.error("业务异常code : " + e.getCode() + ",异常信息: " + e.getMessage(), e);
        } catch (Exception e) {
            commonResult.setCode(ExceptionStatus.E3001001.getCode());
            commonResult.setMessage(ExceptionStatus.E3001001.getMessage());
            log.error("业务异常code : " + ExceptionStatus.E3001001.getCode(), e);
        }

        return commonResult;
    }


    @POST
    @Path("proxy/cal")
    @Override
    public CommonResult<Object> calProxy(ReceiptHeadDto receiptHeadDto) {
        String logSign = "logSign : " + ToolService.getLogSign();

        CommonResult<Object> commonResult = new CommonResult<>();
        try {
            this.checkPara(receiptHeadDto, logSign);

            commonResult = this.orderReceiptService.calProxy(receiptHeadDto, logSign);

            log.info(logSign + "commonResult-calProxy- shippingOrderId {}, 结果 {}" + receiptHeadDto.getShippingOrderId(), JSON.toJSONString(commonResult));
        } catch (BusinessException e) {
            commonResult.setCode(e.getCode());
            commonResult.setMessage(e.getMessage());
            log.error("业务异常code : " + e.getCode() + ",异常信息: " + e.getMessage(), e);
        } catch (Exception e) {
            commonResult.setCode(ExceptionStatus.E3001001.getCode());
            commonResult.setMessage(ExceptionStatus.E3001001.getMessage());
            log.error("业务异常code : " + ExceptionStatus.E3001001.getCode(), e);
        }

        return commonResult;
    }


    @POST
    @Path("batchCreate")
    @Override
    public CommonResult<Object> create(BatchReceiptHeadDto batchReceiptHeadDto) {
        String logSign = "logSign : " + ToolService.getLogSign();

        CommonResult<Object> commonResult = new CommonResult<>();
        try {
            this.checkPara(batchReceiptHeadDto, logSign);

            commonResult = this.orderReceiptService.batchCreateReceiptOrder(batchReceiptHeadDto, logSign);

            log.info(logSign + "commonResult-receipt, 结果 {}", JSON.toJSONString(commonResult));
        } catch (BusinessException e) {
            commonResult.setCode(e.getCode());
            commonResult.setMessage(e.getMessage());
            log.error("业务异常code : " + e.getCode() + ",异常信息: " + e.getMessage(), e);
        } catch (Exception e) {
            commonResult.setCode(ExceptionStatus.E3001001.getCode());
            commonResult.setMessage(ExceptionStatus.E3001001.getMessage());
            log.error("业务异常code : " + ExceptionStatus.E3001001.getCode(), e);
        }

        return commonResult;
    }


    @POST
    @Path("update")
    @Override
    public CommonResult<Object> update(SignHeadDto signHeadDto) {
        String logSign = "logSign : " + ToolService.getLogSign();

        CommonResult<Object> commonResult = new CommonResult<>();
        try {
            this.checkUpdateParams(signHeadDto, logSign);

            commonResult = this.orderReceiptService.updateReceiptOrder(signHeadDto, logSign);

            log.info(logSign + "commonResult-receipt- receiptOrderId {}, 结果 {}" + signHeadDto.getReceiptOrderId(), JSON.toJSONString(commonResult));
        } catch (BusinessException e) {
            commonResult.setCode(e.getCode());
            commonResult.setMessage(e.getMessage());
            log.error("业务异常code : " + e.getCode() + ",异常信息: " + e.getMessage(), e);
        } catch (Exception e) {
            commonResult.setCode(ExceptionStatus.E3001001.getCode());
            commonResult.setMessage(ExceptionStatus.E3001001.getMessage());
            log.error("业务异常code : " + ExceptionStatus.E3001001.getCode(), e);
        }

        return commonResult;
    }

    /**
     * @param receiptHeadDto 参数
     * @return OrderHead
     */
    private boolean checkPara(ReceiptHeadDto receiptHeadDto, String logSign) {

        if (receiptHeadDto == null) {
            throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "请求参数不合法，请检验。");
        }

        log.info(logSign + " :签收入参 －－ {}", JsonUtils.obj2Json(receiptHeadDto));

        if (receiptHeadDto.getShippingOrderId() == null) {
            throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "请求参数不合法，请检验。");
        }

        return true;
    }

    /**
     * @param batchReceiptHeadDto 参数
     * @return OrderHead
     */
    private boolean checkPara(BatchReceiptHeadDto batchReceiptHeadDto, String logSign) {

        if (batchReceiptHeadDto == null) {
            throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "请求参数不合法，请检验。");
        }

        log.info(logSign + " :签收入参 －－ {}", JsonUtils.obj2Json(batchReceiptHeadDto));

        if (batchReceiptHeadDto.getHeadDtos() == null) {
            throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "请求参数不合法，请检验。");
        }
        for (ReceiptHeadDto receiptHeadDto : batchReceiptHeadDto.getHeadDtos()) {
            checkPara(receiptHeadDto, logSign);
        }

        return true;
    }


    /**
     * @param signHeadDto 参数
     */
    private boolean checkUpdateParams(SignHeadDto signHeadDto, String logSign) {

        if (signHeadDto == null) {
            throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "请求参数不合法，请检验。");
        }

        log.info(logSign + " :签收单更新入参 －－ {}", JsonUtils.obj2Json(signHeadDto));

        if (signHeadDto.getReceiptOrderId() == null) {
            throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "签收单号不合法，请检验。");
        }

        if (signHeadDto.getFileds() == null || signHeadDto.getFileds().isEmpty()) {
            throw new BusinessException(ReturnCode.REQUEST_PARAMS_IS_NULL, "更新数据不合法，请检验。");
        }

        return true;
    }
}
